## ----------------------------------------------------------------------------
## Title: 02_registration.R
## Author: Elsa Voytas
## Created: April-9-2024
## ----------------------------------------------------------------------------
library(ggplot2)
library(dplyr)
library(patchwork)
library(rio)
library(stringi)
library(tidyverse)
library(lubridate)
library(forcats)
library(stringi)
library(stringr)
library(readr)
library(tidyr)
library(tibble)

## Producing Figure 2 ("Surviving victim registration trends")

# read in voting data - this is voting data from before registration became automatic
voting <- import("Input/padron2010.dta") # This file contains private, personal data and is not included in replication materials
Encoding(voting$nombre) <-'latin1' # set encoding
voting$nombre <- stri_trans_general(voting$nombre,"Latin-ASCII")

# de-dupe and rename name variable
voting <- voting %>%
  distinct() %>%
  rename(name = nombre,
         rut = cedula,
         dv = digito)

## Creating Figure 1
valech <- read_csv("Input/valech.csv")

# merge by rut and digito
valech.merge <- left_join(valech, voting, by=c("rut","dv"))
valech.merge$finscrip <- as.Date(valech.merge$finscrip, format="%Y%m%d")
valech.merge$yrinscrip <- format(as.Date(valech.merge$finscrip, format="%Y-%m-%d"),"%Y")

reg_surviving <- valech.merge %>%
  mutate(ID = row_number()) %>%
  dplyr::select(ID, yrinscrip) %>%
  rename(year_registered = yrinscrip)

valech.plot <- reg_surviving %>%
  group_by(year_registered) %>%
  dplyr::summarize(count=n())%>%
  mutate(cs=cumsum(count),
         n = nrow(valech)) %>%
  mutate(prop=cs/n) %>%
  filter(!is.na(year_registered)) %>%
  ungroup()

valech.plot$year_registered <- as.numeric(valech.plot$year_registered)

# general population data from Contreras and Navia (2013)
reg_pop <- read_csv("Input/reg_pop.csv")

upper <- ggplot(valech.plot,
            aes(x=year_registered,prop)) +
  geom_point() +
  theme_minimal() +
  geom_line()+
  labs(x="", y = "Proportion registered") +
  theme(axis.text.x=element_blank()) +
  geom_point(data=reg_pop, aes(x=year_registered, y= prop),shape=19) +
  geom_line(data=reg_pop, aes(x=year_registered, y= prop),linetype='dashed') +
  geom_vline(xintercept=2004.5, linetype='dashed', col = 'red') +
  scale_x_continuous(breaks=seq(1987,2010,1), limits = c(1986.5,2010.5)) +
  scale_y_continuous(breaks=seq(0,1,.2), limits =c(0,1)) +
  geom_text(x=1991.5, y=.8, label="Share of general population registered",size=5) +
  geom_text(x=1992.5, y=.35, label="Share of surviving victims registered",size=5) +
  geom_text(x=2004.2, y=0.26, label="Valech Report", angle=90, size=5) +
  theme(text=element_text(size=16))

lower <- ggplot(valech.plot,
            aes(x=year_registered,y=count)) +
  geom_bar(stat="identity") +
  theme_minimal() +
  labs(x="Year", y = "Count of victims registering") +
  theme(axis.text.x=element_text(angle = 90, hjust = 0)) +
  geom_vline(xintercept=2004.5, linetype='dashed', col = 'red') +
  scale_x_continuous(breaks=seq(1987,2010,1), limits = c(1986.5,2010.5)) +
  geom_text(x=2004.2, y=4200, label="Valech Report", angle=90, size=5) +
  theme(text=element_text(size=16))

figure2 <- upper/lower 

ggsave("Output/figure2.pdf", width = 12, height = 6, units="in")
print("Figure 2 complete")

## Producing Figure A3 ("Surviving victim registration trends alongside key developments")
figurea3 <- ggplot(valech.plot,
                   aes(x=year_registered,prop)) +
  geom_point() +
  theme_minimal() +
  geom_line() +
  labs(x="Year", y = "Proportion registered") +
  theme(axis.text.x=element_blank()) +
  geom_vline(xintercept=2004.5, linetype='dashed', col = 'red') +
  geom_vline(xintercept=1990.5, linetype='dashed', col = 'red') +
  geom_vline(xintercept=1991.1, linetype='dashed', col = 'red') +
  geom_vline(xintercept=1998.75, linetype='dashed', col = 'red') +
  geom_vline(xintercept=2003.5, linetype='dashed', col = 'red') +
  geom_vline(xintercept=2007, linetype='dashed', col = 'red') +
  geom_vline(xintercept = 1988.75, linetype="dashed", 
             color = "blue") +
  geom_vline(xintercept = 1990, linetype="dashed", 
             color = "blue") +
  geom_vline(xintercept = 1994, linetype="dashed", 
             color = "blue") +
  geom_vline(xintercept = 2000, linetype="dashed", 
             color = "blue") +
  geom_vline(xintercept = 2006, linetype="dashed", 
             color = "blue") +
  geom_vline(xintercept = 2010, linetype="dashed", 
             color = "blue") +
  scale_x_continuous(breaks=seq(1987,2010,1), limits = c(1986.5,2010.5)) +
  scale_y_continuous(breaks=seq(0,1,.2), limits =c(0,1)) +
  geom_text(x=1990.2, y=0.26, label="Rettig announced", angle=90, size=5) +
  geom_text(x=1990.8, y=0.26, label="Rettig Report", angle=90, size=5) +
  geom_text(x=1998.5, y=0.26, label="Pinochet arrested", angle=90, size=5) +
  geom_text(x=2003.2, y=0.26, label="Valech announced", angle=90, size=5) +
  geom_text(x=2006.7, y=0.26, label="Pinochet dies", angle=90, size=5) +
  geom_text(x=2004.2, y=0.26, label="Valech Report", angle=90, size=5) +
  theme(axis.text.x=element_text(angle = 90, hjust = 0)) +
  theme(text=element_text(size=16))

ggsave("Output/figurea3.pdf", width = 12, height = 6, units="in")
print("Figure A3 complete")

